package com.zmn.plat.services.impl.whitelist;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.whitelist.Whitelist;
import com.zmn.plat.model.entity.whitelist.query.WhitelistQuery;
import com.zmn.plat.persistence.interfaces.whitelist.WhitelistDao;
import com.zmn.plat.services.interfaces.whitelist.WhitelistService;

/**
 * 描述: 白名单接口类实现
 *
 * @author wangjie
 * @since 2019-08-01
 **/
@Service
public class WhitelistServiceImpl implements WhitelistService {

    private WhitelistDao whitelistDao;

    @Autowired
    public WhitelistServiceImpl(WhitelistDao whitelistDao) {
        this.whitelistDao = whitelistDao;
    }

    /**
     * 描述: 根据白名单id 查询数据
     *
     * @param whitelistId
     *            白名单id
     * @param whitelistType
     *            白名单类型
     * @return com.zmn.plat.model.entity.whitelist.Whitelist 白名单
     **/
    @Override
    public Whitelist findByIdAndType(Integer whitelistId, Integer whitelistType) {

        if (Objects.isNull(whitelistId)) {
            throw new NullPointerException("白名单id不能为null");
        }
        if (Objects.isNull(whitelistType)) {
            throw new NullPointerException("白名单类型不能为null");
        }
        return whitelistDao.findByIdAndType(whitelistId, whitelistType);
    }

    /**
     * 描述: 根据白名单类容，类型，查询白名单数据 （ps:如果查询为0条，则返回空的对象，）
     *
     * @param whiteNum
     *            白名单内容
     * @param whiteType
     *            白名单类型
     * @return com.zmn.plat.model.entity.blacklist.Blacklist
     **/
    @Override
    public Whitelist findByWhiteNumAndType(String whiteNum, Integer whiteType) {

        if (Objects.isNull(whiteNum)) {
            throw new NullPointerException("白名单id不能为null");
        }
        if (Objects.isNull(whiteType)) {
            throw new NullPointerException("白名单类型不能为null");
        }
        WhitelistQuery query = new WhitelistQuery();
        query.setWhitelistNum(whiteNum);
        query.setWhitelistType(whiteType);
        return Optional.ofNullable(whitelistDao.listByQuery(query)).orElse(Collections.emptyList()).stream().filter(ff -> Objects.equals(ff.getWhitelistNum(), whiteNum)).findFirst()
            .orElse(new Whitelist());
    }

    /**
     * 描述: 根据条件查询白名单数据 分页
     *
     * @param query
     *            查询条件
     * @return java.util.List<com.zmn.plat.model.entity.whitelist.Whitelist>
     **/
    @Override
    public List<Whitelist> listPageByQuery(WhitelistQuery query) {
        Integer count = whitelistDao.countByQuery(query);
        query.setTotalCount(count);
        return Objects.equals(count, GlobalConsts.NONE) ? Collections.emptyList() : whitelistDao.listPageByQuery(query);
    }

    /**
     * 描述: 根据条件查询白名单数据 不分页
     *
     * @param query
     *            查询条件
     * @return java.util.List<com.zmn.plat.model.entity.whitelist.Whitelist>
     **/
    @Override
    public List<Whitelist> listByQuery(WhitelistQuery query) {
        return whitelistDao.listByQuery(query);
    }

    /**
     * 描述: 根据白名单内容查询数据
     *
     * @param whiteNum
     *            白名单内容
     * @return java.util.List<com.zmn.plat.model.entity.whitelist.Whitelist>
     * @author wangjie
     * @since 2019/8/2
     **/
    @Override
    public List<Whitelist> listByWhiteNum(List<String> whiteNum) {

        if (CollectionUtils.isEmpty(whiteNum)) {
            throw new NullPointerException("白名单内容不能为null或者空集合");
        }
        return whitelistDao.listByWhiteNum(whiteNum);
    }

    /**
     * 描述: 新增白名单
     *
     * @param whitelist
     *            白名单
     **/
    @Override
    public void insertWhitelist(Whitelist whitelist) {

        if (Objects.isNull(whitelist)) {
            throw new NullPointerException("新增时白名单不能为null");
        }
        whitelist.setCreateTime(DateUtil.getNow());
        whitelistDao.insertWhitelist(whitelist);
    }

    /**
     * 描述: 删除白名单数据
     *
     * @param whitelistId
     *            白名单id
     * @param whitelistType
     *            白名单类型
     **/
    @Override
    public void deleteWhitelistByIdAndType(Integer whitelistId, Integer whitelistType) {

        if (Objects.isNull(whitelistId)) {
            throw new NullPointerException("白名单id不能为null");
        }
        if (Objects.isNull(whitelistType)) {
            throw new NullPointerException("白名单类型不能为null");
        }
        whitelistDao.deleteWhitelistByIdAndType(whitelistId, whitelistType);
    }
}
